package com.jake.spring.service.impl;

import com.jake.spring.dao.BeanDao;
import com.jake.spring.service.BeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Service
public abstract class BeanServiceImpl implements BeanService {

    @Autowired
    @Qualifier("singletonDao")
    private BeanDao singletonDao;

    @Autowired
    @Qualifier("prototypeDao")
    private BeanDao prototypeDao;

    @Autowired
    private ApplicationContext applicationContext;

    @Lookup("prototypeDao")
    protected abstract BeanDao getPrototypeDao();

    @Override
    public int getHashCodeOfDao(boolean isSingleton) {
        if (isSingleton) {
            return singletonDao.queryHashCode();
        }
        return prototypeDao.queryHashCode();
    }

    @Override
    public int getHashCodeOfDaoByApplicationContext(boolean isSingleton) {
        String beanName = "prototypeDao";
        if (isSingleton) {
            beanName = "singletonDao";
        }
        BeanDao beanDao = (BeanDao) applicationContext.getBean(beanName);
        return beanDao.queryHashCode();
    }

    @Override
    public int lookupHashCodeOfDao() {
        return getPrototypeDao().queryHashCode();
    }

}
